package com.example.javaee.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.javaee.common.result;
import com.example.javaee.entity.Asset;
import com.example.javaee.entity.AssetLabel;
import com.example.javaee.entity.AssetOrg;
import com.example.javaee.entity.User;
import com.example.javaee.service.AssetLabelService;
import com.example.javaee.service.AssetOrgService;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/assetLabel")
public class AssetLabelController {
    @Autowired
    private AssetLabelService assetLabelService;

    //获取所有标签
    @GetMapping("/getAll")
    public result<List<AssetLabel>> getAll(HttpServletRequest request) {
        LambdaQueryWrapper<AssetLabel> queryWrapper = new LambdaQueryWrapper<>();
        // 执行查询
        List<AssetLabel> assetLabelList = assetLabelService.list(queryWrapper);
        return result.success(assetLabelList,"获取成功");

    }

    //获取标签名字
    @GetMapping("/getByid/{id}")
    public result<AssetLabel> getByid(HttpServletRequest request,@PathVariable Integer id) {
        // 执行查询
        AssetLabel assetLabel= assetLabelService.getById(id);
        return result.success(assetLabel,"获取成功");
    }

    @PostMapping("/add")
    public  result<String> register(@RequestBody AssetLabel assetlabel){
        LambdaQueryWrapper<AssetLabel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(AssetLabel::getLabelName, assetlabel.getLabelName());
        if (assetLabelService.getOne(queryWrapper)!=null){
            return result.error("标签已存在");
        }
        assetLabelService.save(assetlabel);
        return result.success(null,"添加成功");

    }

    @DeleteMapping("/{id}")
    public result<String> delete(@PathVariable Integer id) {
        if(assetLabelService.removeById(id)){
            return  result.success(null,"删除成功");}
        else{
            return result.error("删除失败");
        }
    }

}
